<?php
/**
 * --------------------------------------------------------------
 * index模型
 * Author: zd <kfzphp@126.com>
 * Date: 2018-06-01
 * Project: kfzphp.com
 * --------------------------------------------------------------
 */

class IndexModel extends EntryModel
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 获取商品分类总数
     * @param  [type]     $where [description]
     * @return [type]            [description]
     */
    public function getCateCount($where)
    {
        $sql = 'select count(id) from ' . tab('goods_category') . $where;
        return $this->mysql->getOne($sql);
    }

    /**
     * 获取商品分类列表
     * @param  [type]     $where [description]
     * @param  [type]     $limit [description]
     * @return [type]            [description]
     */
    public function getCateList($where, $limit)
    {
        $sql = 'select id as cate_id, name as cate_name, image as cate_image, sort as cate_sort, sort as cate_sort ' .
               'from ' . tab('goods_category') . $where . ' ' .
               'order by create_time desc ' .
               'limit ' . $limit;
        return $this->mysql->getAll($sql);
    }

    /**
     * 新增商品分类
     * @param  [type]     $data [description]
     * @return [type]           [description]
     */
    public function insertCate($data)
    {
        return $this->mysql->insert(tab('goods_category'), $data);
    }

    /**
     * 删除商品分类
     * @param  [type]     $cate_id [description]
     * @return [type]              [description]
     */
    public function deleteCateById($cate_id)
    {
        $sql = 'delete from ' . tab('goods_category') . ' where id = ' . $cate_id;
        return $this->mysql->delete($sql);
    }

    /**
     * 获取商品分类信息
     * @param  [type]     $cate_id [description]
     * @return [type]              [description]
     */
    public function getCateById($cate_id)
    {
        $sql = 'select id, name, image, sort from ' . tab('goods_category') . ' where id = ' . $cate_id;
        return $this->mysql->getRow($sql);
    }

    /**
     * 更新商品分类信息
     * @param  array      $data  [description]
     * @param  string     $where [description]
     * @return [type]            [description]
     */
    public function updateCate($data = array(), $where = '')
    {
        return $this->mysql->update(tab('goods_category'), $data, $where);
    }

    /**
     * 判断该分类下是否存在商品
     * @param  [type]     $cate_id [description]
     * @return [type]              [description]
     */
    public function getGoodsCountByCateid($cate_id)
    {
        $sql = 'select count(id) from ' . tab('goods') . ' where category_id = ' . $cate_id;
        return $this->mysql->getOne($sql);
    }

}